using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Ent.Voice;
using Ent.Web.Helper;
using System.Collections.Generic;
public partial class Controls_Common_PostMessage : System.Web.UI.UserControl
{
    public bool _ShowUser = true;
    public bool ShowUser
    {
        get { return _ShowUser; }
        set { _ShowUser = value; }
    }
    public int _messageHeight = 150;
    public int MessageHeight
    {
        get { return _messageHeight; }
        set { _messageHeight = value; }
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        txtMessage.Height = Unit.Pixel(_messageHeight);
        string to = Request.QueryString["id"];
        if (Session["USERID"] != null)
        {
            if (to == Session["USERID"].ToString())
            {
                ShowSelfMessage();
                return;
            }
        }
        if (ShowUser)
        {
            para_user.Visible = true;
            List<string> adminIds = new List<string>();
            adminIds.AddRange(new string[] { "admin", "-1", "vadmin", "" });
            if (to == null || adminIds.Contains(to.ToLower()))
                to = ConfigurationManager.AppSettings["VibzAdmin"];
            lblUser.Text = Common.GetImageTextLink("ShowUser.aspx?id=" + to, "Images/dj/user.gif", StringHelper.RestrictMaxChar((to == ConfigurationManager.AppSettings["VibzAdmin"] ? ConfigurationManager.AppSettings["fromEmailDisplayName"] : to), 20), 15);
        }
        else
            para_user.Visible = false;
        if (!IsPostBack)
        {
            lblSubject.Visible = true;
            lblMessage.Visible = true;
            txtMessage.Visible = true;
            txtSubject.Visible = true;
            btnSubmit.Visible = true;
            rfvSubject.ErrorMessage = ResourceHelper.Get("ALERT_SubjectMandatory");
            rfvMessage.ErrorMessage = ResourceHelper.Get("ALERT_ContentMandatory");

            lblSubject.Text = ResourceHelper.Get("LBL_Subject");
            lblMessage.Text = ResourceHelper.Get("LBL_Message");

            btnSubmit.ImageUrl = ConfigurationManager.AppSettings["BaseUrl"] + "/Images/dj/go.gif";
            btnSubmit.Attributes.Add("onmouseover", "this.src='" + ConfigurationManager.AppSettings["BaseUrl"] + "/Images/dj/goh.gif'");
            btnSubmit.Attributes.Add("onmouseout", "this.src='" + ConfigurationManager.AppSettings["BaseUrl"] + "/Images/dj/go.gif'");
        }
    }
    protected void btnSubmit_Click(object sender, ImageClickEventArgs e)
    {
        if (Session["USERID"] != null)
        {
            Message msg = new Message();
            msg.FromUserId = Session["USERID"].ToString();
            string to = Request.QueryString["id"];
            List<string> adminIds = new List<string>();
            adminIds.AddRange(new string[] { "admin", "-1", "vadmin", "" });
            if (to == null || adminIds.Contains(to.ToLower()))
                to = ConfigurationManager.AppSettings["VibzAdmin"];
        
            if (to == msg.FromUserId)
            {
                ShowSelfMessage();
                return;
            }
            msg.ToUserId = to;
            msg.Content = HttpUtility.HtmlEncode(txtMessage.Text);
            msg.Subject = txtSubject.Text;
            Message.Post(msg);
            Response.Redirect(ConfigurationManager.AppSettings["BaseUrl"] + "/Info.aspx?t=3&c=MessagePosted", false);
        }
        else
        {
            Response.Redirect(ConfigurationManager.AppSettings["BaseUrl"] + "/DoLogin.aspx?msg=Login Required.&url=" + HttpUtility.UrlEncode(Request.Url.PathAndQuery), false);
        }
    }
    void ShowSelfMessage()
    {
        lblInfo.Text = Common.GetErrorText(ResourceHelper.Get("MSG_SelfMessage")) + "<br/><hr/>";
        lblInfo.Visible = true;
        lblSubject.Visible = false;
        lblMessage.Visible = false;
        txtMessage.Visible = false;
        txtSubject.Visible = false;
        btnSubmit.Visible = false;
    }
}
